﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Xml;
using Assist;
using FlexCell;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;

namespace YJKMaterialStatistic
{
	public partial class DlgColumnSetCommon : System.Windows.Forms.Form
	{
		public DlgColumnSetCommon(string strGridName, string strFolderPath, List<string> lstColumnInfo, ref GridSetCommonInfo gridSetInfo)
		{
			this.InitializeComponent();
			this.m_gridSetInfo = gridSetInfo;
			this.m_strFilePath = strFolderPath;
			this.m_strFilePath = this.m_strFilePath + "\\" + strGridName + ".hymb";
			this.GetXYDpi(ref this.m_dpiX, ref this.m_dpiY);
			this.SetGridTemplate();
			this.GetColumnInfo(lstColumnInfo);
			List<string> list = new List<string>();
			for (int i = 0; i < lstColumnInfo.Count; i++)
			{
				DataGridViewRow dataGridViewRow = new DataGridViewRow();
				dataGridViewRow.CreateCells(this.dataGridView1, new object[]
				{
					lstColumnInfo[i],
					this.PixelToMM(this.m_gridSetInfo.LstColumnWidth[i]).ToString("0")
				});
				this.dataGridView1.Rows.Add(dataGridViewRow);
				dataGridViewRow.Cells[0].ReadOnly = true;
				if (list.IndexOf(lstColumnInfo[i]) != -1)
				{
					dataGridViewRow.Visible = false;
				}
				else
				{
					list.Add(lstColumnInfo[i]);
				}
			}
			this.txtFontSize.Text = this.FontUnitToMM(this.m_gridSetInfo.FontSize).ToString("0.00");
			foreach (FontFamily fontFamily in FontFamily.Families)
			{
				this.cmbFontName.Items.Add(fontFamily.Name);
			}
		}

		public DlgColumnSetCommon(string strVersion, string strGridName, string strFolderPath, List<string> lstColumnInfo, ref GridSetCommonInfo gridSetInfo)
		{
			this.InitializeComponent();
			this.m_gridSetInfo = gridSetInfo;
			this.m_strFilePath = strFolderPath;
			this.m_strFilePath = this.m_strFilePath + "\\" + strGridName + ".hymb";
			this.GetXYDpi(ref this.m_dpiX, ref this.m_dpiY);
			this.SetGridTemplate();
			this.GetColumnInfo(lstColumnInfo);
			List<string> list = new List<string>();
			for (int i = 0; i < lstColumnInfo.Count; i++)
			{
				DataGridViewRow dataGridViewRow = new DataGridViewRow();
				dataGridViewRow.CreateCells(this.dataGridView1, new object[]
				{
					lstColumnInfo[i],
					this.PixelToMM(this.m_gridSetInfo.LstColumnWidth[i]).ToString("0")
				});
				this.dataGridView1.Rows.Add(dataGridViewRow);
				dataGridViewRow.Cells[0].ReadOnly = true;
				if (list.IndexOf(lstColumnInfo[i]) != -1)
				{
					dataGridViewRow.Visible = false;
				}
				else
				{
					list.Add(lstColumnInfo[i]);
				}
			}
			foreach (FontFamily fontFamily in FontFamily.Families)
			{
				this.cmbFontName.Items.Add(fontFamily.Name);
			}
			this.m_strRevitVersion = strVersion;
			this.SetFormInitData();
		}

		private void SetFormInitData()
		{
			if (this.m_gridSetInfo.FontName == "")
			{
				this.cmbFontName.Text = "仿宋";
				if (2016 < Convert.ToInt32(this.m_strRevitVersion))
				{
					this.txtFontSize.Text = "2.50";
					this.txtTitleFontSize.Text = "3.00";
				}
				else
				{
					this.txtFontSize.Text = "3.00";
					this.txtTitleFontSize.Text = "3.5";
				}
				for (int i = 1; i < 17; i++)
				{
					this.cmbLineWidthOut.Items.Add(i.ToString());
					this.cmbLineWidthInner.Items.Add(i.ToString());
				}
				this.cmbLineWidthOut.Text = "7";
				this.cmbLineWidthInner.Text = "2";
				return;
			}
			this.cmbFontName.Text = this.m_gridSetInfo.FontName;
			this.txtFontSize.Text = this.FontUnitToMM(this.m_gridSetInfo.FontSize).ToString("0.00");
			this.txtTitleFontSize.Text = this.FontUnitToMM(this.m_gridSetInfo.TitleFontSize).ToString("0.00");
			for (int j = 1; j < 17; j++)
			{
				this.cmbLineWidthOut.Items.Add(j.ToString());
				this.cmbLineWidthInner.Items.Add(j.ToString());
			}
			this.cmbLineWidthOut.Text = this.m_gridSetInfo.LineWidthOut.ToString();
			this.cmbLineWidthInner.Text = this.m_gridSetInfo.LineWidthInner.ToString();
		}

		private void SetGridTemplate()
		{
			if (!this.grid1.LoadFromXML(this.m_strFilePath))
			{
				this.grid1.Column(1).Width = 100;
				this.grid1.Cell(1, 1).Text = "表格预览加载失败!";
			}
			this.grid1.ScrollBars = ScrollBarsEnum.Both;
			this.grid1.Row(0).Visible = false;
			this.grid1.Column(0).Visible = false;
			this.grid1.Locked = true;
			this.grid1.ShowResizeTip = true;
			this.grid1.Refresh();
			this.grid1.BackColorBkg = System.Drawing.Color.White;
		}

		private void GetColumnInfo(List<string> lstColumnInfo)
		{
			this.m_gridSetInfo.LstColumnWidth.Clear();
			for (int i = 0; i < lstColumnInfo.Count; i++)
			{
				this.m_gridSetInfo.LstColumnWidth.Add(this.grid1.Column(i + 1).Width);
			}
		}

		public void GetFontInfo()
		{
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.Load(this.m_strFilePath);
			XmlElement xmlElement = xmlDocument.DocumentElement.SelectSingleNode("GridInfo") as XmlElement;
			if (xmlElement == null)
			{
				return;
			}
			this.m_gridSetInfo.FontSize = Convert.ToSingle(xmlElement.GetAttribute("FontSize"));
			this.m_gridSetInfo.TitleFontSize = Convert.ToSingle(xmlElement.GetAttribute("TitleFontSize"));
			this.m_gridSetInfo.FontName = xmlElement.GetAttribute("FontName");
			this.m_gridSetInfo.LineWidthOut = Convert.ToInt32(xmlElement.GetAttribute("LineWidthOut"));
			this.m_gridSetInfo.LineWidthInner = Convert.ToInt32(xmlElement.GetAttribute("LineWidthInner"));
		}

		private void DlgGridColumnSet_Load(object sender, EventArgs e)
		{
			//base.Icon = Resource.YArchitechIcon;
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			this.m_gridSetInfo.LstColumnWidth.Clear();
			for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
			{
				string strName = this.dataGridView1.Rows[i].Cells[0].Value.ToString();
				string value = this.dataGridView1.Rows[i].Cells[1].Value.ToString();
				if (this.dataGridView1.Rows[i].Visible)
				{
					this.m_gridSetInfo.LstColumnWidth.Add(this.MMToPixel(Convert.ToDouble(value)));
				}
				else
				{
					int sameItemIndex = this.GetSameItemIndex(strName);
					if (sameItemIndex != -1)
					{
						this.m_gridSetInfo.LstColumnWidth.Add(this.MMToPixel(Convert.ToDouble(this.dataGridView1.Rows[sameItemIndex].Cells[1].Value.ToString())));
					}
				}
			}
			this.m_gridSetInfo.FontSize = this.MMToFontUnit(Convert.ToDouble(this.txtFontSize.Text));
			this.m_gridSetInfo.FontName = this.cmbFontName.Text;
			this.m_gridSetInfo.TitleFontSize = this.MMToFontUnit(Convert.ToDouble(this.txtTitleFontSize.Text));
			this.m_gridSetInfo.LineWidthOut = Convert.ToInt32(this.cmbLineWidthOut.Text);
			this.m_gridSetInfo.LineWidthInner = Convert.ToInt32(this.cmbLineWidthInner.Text);
			this.m_gridSetInfo.SetColWidth = true;
			this.SaveGridTemplate();
			base.Close();
		}

		private int GetSameItemIndex(string strName)
		{
			int result = -1;
			for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
			{
				if (this.dataGridView1.Rows[i].Cells[0].Value.ToString() == strName)
				{
					return i;
				}
			}
			return result;
		}

		private void SaveGridTemplate()
		{
			if (this.m_gridSetInfo.LstColumnWidth.Count <= 0)
			{
				return;
			}
			List<string> textDataByOtherVersion = this.GetTextDataByOtherVersion();
			this.grid1.Cell(1, 1).FontSize = this.m_gridSetInfo.TitleFontSize;
			this.grid1.Range(2, 1, this.grid1.Rows - 1, this.grid1.Cols - 1).FontSize = this.m_gridSetInfo.FontSize;
			this.grid1.Range(1, 1, this.grid1.Rows - 1, this.grid1.Cols - 1).FontName = this.m_gridSetInfo.FontName;
			for (int i = 0; i < this.m_gridSetInfo.LstColumnWidth.Count; i++)
			{
				this.grid1.Column(i + 1).Width = this.m_gridSetInfo.LstColumnWidth[i];
			}
			for (int i = 1; i < this.grid1.Rows; i++)
			{
				this.grid1.Row(i).AutoFit();
			}
			this.grid1.ExportToXML(this.m_strFilePath);
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.Load(this.m_strFilePath);
			XmlElement documentElement = xmlDocument.DocumentElement;
			XmlElement xmlElement = xmlDocument.CreateElement("GridInfo");
			documentElement.AppendChild(xmlElement);
			xmlElement.SetAttribute("RevitVersion", this.m_strRevitVersion);
			xmlElement.SetAttribute("FontSize", this.FontUnitToMM(this.m_gridSetInfo.FontSize).ToString());
			xmlElement.SetAttribute("TitleFontSize", this.FontUnitToMM(this.m_gridSetInfo.TitleFontSize).ToString());
			xmlElement.SetAttribute("FontName", this.m_gridSetInfo.FontName);
			xmlElement.SetAttribute("LineWidthOut", this.m_gridSetInfo.LineWidthOut.ToString());
			xmlElement.SetAttribute("LineWidthInner", this.m_gridSetInfo.LineWidthInner.ToString());
			if (textDataByOtherVersion.Count != 0)
			{
				xmlElement = xmlDocument.CreateElement("GridInfo");
				documentElement.AppendChild(xmlElement);
				xmlElement.SetAttribute("RevitVersion", textDataByOtherVersion[0]);
				xmlElement.SetAttribute("FontSize", textDataByOtherVersion[1]);
				xmlElement.SetAttribute("TitleFontSize", textDataByOtherVersion[2]);
				xmlElement.SetAttribute("FontName", textDataByOtherVersion[3]);
				xmlElement.SetAttribute("LineWidthOut", textDataByOtherVersion[4]);
				xmlElement.SetAttribute("LineWidthInner", textDataByOtherVersion[5]);
			}
			xmlDocument.Save(this.m_strFilePath);
		}

		private List<string> GetTextDataByOtherVersion()
		{
			List<string> list = new List<string>();
			try
			{
				XmlDocument xmlDocument = new XmlDocument();
				xmlDocument.Load(this.m_strFilePath);
				XmlNodeList xmlNodeList = xmlDocument.DocumentElement.SelectNodes("GridInfo");
				if (xmlNodeList.Count == 0)
				{
					return list;
				}
				foreach (object obj in xmlNodeList)
				{
					XmlElement xmlElement = ((XmlNode)obj) as XmlElement;
					string attribute = xmlElement.GetAttribute("RevitVersion");
					if ((!("2016" == attribute) || !(this.m_strRevitVersion == "2016")) && (!("2016" != attribute) || !(this.m_strRevitVersion != "2016")))
					{
						list.Add(xmlElement.GetAttribute("RevitVersion"));
						list.Add(xmlElement.GetAttribute("FontSize"));
						list.Add(xmlElement.GetAttribute("TitleFontSize"));
						list.Add(xmlElement.GetAttribute("FontName"));
						list.Add(xmlElement.GetAttribute("LineWidthOut"));
						list.Add(xmlElement.GetAttribute("LineWidthInner"));
						break;
					}
				}
			}
			catch
			{
			}
			return list;
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void GetXYDpi(ref double Xdpi, ref double Ydpi)
		{
			using (Graphics graphics = Graphics.FromHwnd(IntPtr.Zero))
			{
				Xdpi = (double)graphics.DpiX;
				Ydpi = (double)graphics.DpiY;
			}
		}

		private double PixelToMM(short value)
		{
			return (double)value / this.m_dpiY * 0.08333 * 304.8;
		}

		private short MMToPixel(double value)
		{
			return Convert.ToInt16(value * this.m_dpiY / 0.08333 / 304.8);
		}

		private double FontUnitToMM(float value)
		{
			return (double)(value / 14f / 72f) * 304.8;
		}

		private float MMToFontUnit(double value)
		{
			return Convert.ToSingle(value * 14.0 * 72.0 / 304.8);
		}

		private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
		{
			if (this.dataGridView1.CurrentCell.ColumnIndex != 0)
			{
				System.Windows.Forms.TextBox textBox = e.Control as System.Windows.Forms.TextBox;
				textBox.KeyPress -= this.tx_KeyPress;
				textBox.KeyPress += this.tx_KeyPress;
				return;
			}
			System.Windows.Forms.Control control = e.Control;
		}

		private void tx_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (!char.IsNumber(e.KeyChar) && e.KeyChar != '\r' && e.KeyChar != '\b')
			{
				e.Handled = true;
			}
		}

		private void DlgColumnSetCommon_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Escape)
			{
				base.Close();
			}
		}

		private double m_dpiX;

		private double m_dpiY;

		private string m_strFilePath = ";";

		private GridSetCommonInfo m_gridSetInfo;

		private string m_strRevitVersion = "";
	}
}
